package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class logAspect {
    /*
    2.前置通知
     */
    /*@Before(value = "execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
    public void beforeMethod(JoinPoint joinPoint){
        *//*
        JoinPoint参数，表示当前正在执行的目标方法
        1.获取目标方法的名称
         *//*
        String name = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("[前置通知]:"+name+"方法开始，参数为"+args);
    }*/
    /*
    2.返回通知AfterReturning
     */
    /*@AfterReturning(value = "execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))",returning = "r")
    public void afterReturningMethod(JoinPoint joinPoint,Object r){
        String name = joinPoint.getSignature().getName();
        System.out.println("[返回通知:]"+name+"方法结束，结果为："+r);
    }*/
    /*
    3.后置通知
     */
    /*@After(value = "execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
    public void afterMethod(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("[后置通知:]"+name+"方法彻底结束");
    }*/
    /*
    4.异常通知：目标方法发生异常是执行，能欧获取到具体的异常对象Exception
     */
    /*@AfterThrowing(value = "execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Throwable ex){
        String name = joinPoint.getSignature().getName();
        String message = ex.getMessage();
        System.out.println("[异常通知]:"+name+"方法异常："+message);
    }*/
    /*
    5.环绕通知
     */
    @Around(value = "execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        Object result=null;
        try{
            //前置通知
            System.out.println("1.前置通知");
            result=joinPoint.proceed();
            //返回通知
            System.out.println("2.返回通知");
        }
        catch (Throwable e) {
            System.out.println("3.异常通知");
            throw new RuntimeException(e);
        }finally {
            System.out.println("4.后置通知");
        }
        return result;
    }

}